package com.yaorange.jk.web.cargo;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ModelDriven;
import com.yaorange.jk.entity.PackingList;
import com.yaorange.jk.service.PackingListService;
import com.yaorange.jk.utils.Pagination;
import com.yaorange.jk.web.BaseAction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import java.util.List;


@Controller("packingListAction")
@Scope("prototype")
public class PackingListAction extends BaseAction implements ModelDriven<PackingList>  {

    @Autowired
    private PackingListService packingListService;

    private Pagination page=new Pagination();

    private List<PackingList> packingListList;

    private PackingList model= new PackingList();

    public String list(){

    Pagination pagination =  packingListService.findAllPage(page);
    pagination.setUrl("packingListAction_list");
    push(pagination);
    return "list";
    }

    public String tocreate(){
    //packingListList = packingListService.findAll();
    //        ActionContext.getContext().getValueStack().set("packingListList",packingListList);
        page =  packingListService.findSubmitExportPage(page);
        page.setUrl("packingListAction_tocreate");
        push(page);
    return "tocreate";
    }

    public String insert(){
        model.setState(0d);
        packingListService.save(model);
        return list();
    }
    //提交装箱单
    public String toPacking(){
        packingListService.toPacking(model);
        return "rlist";
    }
    //取消装箱单
    public String toCancel(){
        packingListService.toCancel(model);
        return "rlist";
    }
    public String toupdate(){
    PackingList packingList = packingListService.findById(model.getId());
    super.push(packingList);

//    packingListList = packingListService.findAll();
//    packingListList.remove(packingList);
    return "toupdate";
    }

    public String update(){
    packingListService.update(model);
    return list();
    }

    /**
    * @return
    */
    public String delete(){
    String[] ids = model.getId().split(", ");
    for (String id:ids) {
    packingListService.deleteById(model.getId());
    }

    return list();
    }

    public String toview()
    {
    PackingList packingList = packingListService.findById(model.getId());
    push(packingList);
    return "toview";
    }
    public void setPage(Pagination page) {
    this.page = page;
    }

    public List<PackingList> getPackingListList() {
    return packingListList;
    }

    @Override
    public PackingList getModel() {
    return model;
    }
}
